package com.wen.controller;

import com.wen.common.ResultDto;
import com.wen.common.ResultDtoFactory;
import com.wen.controller.dto.UserDto;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * HTML 标签
 */
@Controller
@RequestMapping("htmlElement")
public class HtmlElementController {

    @RequestMapping("index")
    public String index() {
        return "htmlElement/index";
    }

    @ResponseBody
    @RequestMapping("ajax")
    public ResultDto ajaxData(String name) {
        ResultDto resultDto = ResultDtoFactory.ERROR();
        resultDto.setData(name);

        return resultDto;
    }

    @RequestMapping("div")
    public String div() {
        return "htmlElement/div-del";
    }

    /**
     * 显示表格，相同名称合并
     *
     * @param model
     * @return
     */
    @RequestMapping("table")
    public String table(ModelMap model) {
        List<UserDto> userDtoList = new ArrayList<UserDto>();

        UserDto userDto1 = new UserDto();
        userDto1.setUserName("胡军");
        userDto1.setPassword("123789");
        userDto1.setAge(23);

        UserDto userDto2 = new UserDto();
        userDto2.setUserName("胡小明");
        userDto2.setPassword("58695");
        userDto2.setAge(33);

        UserDto userDto3 = new UserDto();
        userDto3.setUserName("石头");
        userDto3.setPassword("148857555");
        userDto3.setAge(20);

        UserDto userDto4 = new UserDto();
        userDto4.setUserName("石头");
        userDto4.setPassword("3259546");
        userDto4.setAge(21);

        UserDto userDto5 = new UserDto();
        userDto5.setUserName("石头");
        userDto5.setPassword("123789");
        userDto5.setAge(18);

        UserDto userDto6 = new UserDto();
        userDto6.setUserName("石头");
        userDto6.setPassword("12372539889");
        userDto6.setAge(26);

        UserDto userDto7 = new UserDto();
        userDto7.setUserName("小夏");
        userDto7.setPassword("12372539889");
        userDto7.setAge(26);

        userDtoList.add(userDto1);
        userDtoList.add(userDto2);
        //userDtoList.add(userDto3);
        userDtoList.add(userDto4);
        userDtoList.add(userDto5);
        userDtoList.add(userDto6);
        userDtoList.add(userDto7);

        model.addAttribute("userDtoList", userDtoList);
        return "htmlElement/table";
    }

    /**
     * 搜索
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("search")
    public Map autoSearch(String keyword) {
        System.out.println("----------" + keyword);

        List<Map> list = new ArrayList<Map>();

        Map map = new HashMap();
        map.put("title", "中国会好1");
        map.put("id", "11");
        list.add(map);

        Map map1 = new HashMap();
        map1.put("title", "反对法墙砖2");
        map1.put("id", "12");
        list.add(map1);

        Map map2 = new HashMap();
        map2.put("title", "中城墙承城地圾");
        map2.put("id", "13");
        list.add(map2);

        Map resultMap = new HashMap();
        resultMap.put("data", list);

        return resultMap;
    }

    /**
     * 树型结构操作
     *
     * @return
     */
    @RequestMapping("tree")
    public String tree() {
        return "htmlElement/tree";
    }

    @ResponseBody
    @RequestMapping("nodes")
    public List<Object> nodes() {
        List<Object> returnList = new ArrayList<Object>();
        Map map = new HashMap();
        map.put("id", "101");
        map.put("name", "中策橡胶集团");

        List<Map> childrens = new ArrayList<Map>();
        Map map1 = new HashMap();
        map1.put("id", "102");
        map1.put("name", "中策小胎");
        childrens.add(map1);

        List<Map> childrens1 = new ArrayList<Map>();
        Map map3 = new HashMap();
        map3.put("id", "104");
        map3.put("name", "华东科");
        childrens1.add(map3);

        map1.put("children",childrens1);

        Map map2 = new HashMap();
        map2.put("id", "103");
        map2.put("name", "中策大胎");
        childrens.add(map2);

        map.put("children",childrens);

        returnList.add(map);

        return returnList;
    }


}
